package com.bootdo.market.controller;

import com.alibaba.fastjson.JSON;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.market.dao.CommonDao;
import com.bootdo.market.dao.DuplicateDao;
import com.bootdo.market.domain.ProductChannelDO;
import com.bootdo.market.service.ProductChannelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.bootdo.common.utils.TimeUtils.getSecondTimestampTwo;

/**
 * @author jinse
 * @email market@jinse.com
 * @date 2019-04-12 18:15:52
 */

@Controller
@RequestMapping("/market/productChannel")
public class ProductChannelController {
    @Autowired
    private ProductChannelService productChannelService;
    @Autowired
    private CommonDao commonDao;
    @Autowired
    private DuplicateDao duplicateDao;


    @GetMapping()
    @RequiresPermissions("market:productChannel:productChannel")
    String ProductChannel() {
        return "market/productChannel/productChannel";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("market:productChannel:productChannel")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ProductChannelDO> productChannelList = productChannelService.list(query);
        int total = productChannelService.count(query);
        PageUtils pageUtils = new PageUtils(productChannelList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("market:productChannel:add")
    String add() {
        return "market/productChannel/add";
    }

    @RequestMapping(value = "/listAllProduct", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String listAllProduct(HttpServletRequest request, ProductChannelDO productChannel) {
        List<Map<String, Object>> channelList = productChannelService.listAllProduct();
        List<Map<String, Object>> userProductList = commonDao.findProductByChannelid(productChannel.getChannelid());
        List<Map<String, Object>> aalist = channelList.stream().filter(stringObjectMap ->
                userProductList.stream().filter(stringObjectMap1 ->
                        stringObjectMap.get("id").toString().equals(stringObjectMap1.get("productId").toString())
                ).count() == 0).collect(Collectors.toList());
        return JSON.toJSONString(channelList);
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("market:productChannel:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        ProductChannelDO productChannel = productChannelService.get(id);
        model.addAttribute("productChannel", productChannel);
        return "market/productChannel/edit";
    }

    /**
     * 保存
     */
    @Transactional
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("market:productChannel:add")
    public R save(ProductChannelDO productChannel) {
        productChannel.setChannelid(getSecondTimestampTwo(System.currentTimeMillis()));
        if (productChannelService.save(productChannel) > 0) {
         /*   String[] aa = productChannel.getProductId().split(",");
            for (int i = 0; i < aa.length; i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("channelId", productChannel.getChannelid());
                map.put("productId", Integer.parseInt(aa[i]));
                String url = "http://" + productService.get(Integer.parseInt(aa[i])).getProductdomain() + "?channelId=" + productChannel.getChannelid();
                map.put("url", url);
                aacommonService.saveProductChannel(map);
            }*/

            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("market:productChannel:edit")
    public R update(ProductChannelDO productChannel) {
        productChannelService.update(productChannel);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("market:productChannel:remove")
    public R remove(ProductChannelDO productChannel) {
        productChannelService.update(productChannel);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("market:productChannel:batchRemove")
    public R remove(@RequestParam("ids[]") Integer[] ids) {
        productChannelService.batchRemove(ids);
        return R.ok();
    }

    @RequestMapping(value = "/productChannelUniqueAdd", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public R productChannelUniqueAdd(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        Map<String, Object> aa = duplicateDao.productChannelUniqueAdd(params);
        if (aa == null) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping(value = "/productChannelUniqueEdit", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public R productChannelUniqueEdit(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        Map<String, Object> aa = duplicateDao.productChannelUniqueEdit(params);
        if (aa == null) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping(value = "/listChannelTemplate", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String listChannelTemplate(HttpServletRequest request, ProductChannelDO productChannel) {
        List<Map<String, Object>> userProductList = commonDao.listChannelTemplate(productChannel);
        if (userProductList != null && userProductList.size() > 0) {
            userProductList.stream().forEach(stringObjectMap -> {
                stringObjectMap.put("productName", stringObjectMap.get("channelName") + "  :  " + stringObjectMap.get("productName"));
            });
        }
        return JSON.toJSONString(userProductList);
    }

    @RequestMapping(value = "/listAllProductChannel", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String listAllProductChannel(HttpServletRequest request, ProductChannelDO productChannel) {
        List<Map<String, Object>> channelList = commonDao.listAllChannel();
        List<Map<String, Object>> userProductList = commonDao.listUseProductChannel();
        List<Map<String, Object>> aalist = channelList.stream().filter(stringObjectMap ->
                userProductList.stream().filter(stringObjectMap1 ->
                        stringObjectMap.get("channelId").toString().equals(stringObjectMap1.get("channelId").toString())
                ).count() == 0).collect(Collectors.toList());
        return JSON.toJSONString(aalist);
    }

}
